1 00:00:00,000 --> 00:00:14,640 This is Hacker Public Radio Episode 3,882 for Tuesday, the 20th of June 2023. 2 00:00:14,640 --> 00:00:19,480 Today's show is entitled, Alternatives to the CD Command. 3 00:00:19,480 --> 00:00:22,320 It is part of the series Bash Scripting. 4 00:00:22,320 --> 00:00:26,680 It is hosted by Clotoo and is about 11 minutes long. 5 00:00:26,680 --> 00:00:29,280 It carries a clean flag. 6 00:00:29,280 --> 00:00:40,800 The summary is Navigate your file system without CD. 7 00:00:40,800 --> 00:00:45,080 Hey everybody, this is Clotoo and this episode I want to talk about changing directories. 8 00:00:45,080 --> 00:00:49,360 I know that's not seemingly a very exciting topic, but what I want to do in this episode 9 00:00:49,360 --> 00:00:55,960 is come up with as many ways I can think of to navigate a system in Linux without CD or 10 00:00:55,960 --> 00:00:58,880 not in the way that you would expect with CD. 11 00:00:58,880 --> 00:01:02,720 So the first one that I could think of was Push-D and Pop-D. 12 00:01:02,720 --> 00:01:10,760 In the Bash Shell, the Push-D, Pop-D and Ders, D-I-R-S commands manage a map of everywhere 13 00:01:10,760 --> 00:01:12,480 you go on your system. 14 00:01:12,480 --> 00:01:18,320 I mean they only do that when you use them so you have to mentally substitute Push-D instead 15 00:01:18,320 --> 00:01:21,120 of CD or you can alias it, something like that. 16 00:01:21,120 --> 00:01:22,120 But here's how it works. 17 00:01:22,120 --> 00:01:28,040 When you want to change directorie to a new location, use Push-D to push your current 18 00:01:28,040 --> 00:01:30,440 location onto the stack. 19 00:01:30,440 --> 00:01:35,440 The stack in this context is an imaginary play, well it's not imaginary, it's a place 20 00:01:35,440 --> 00:01:38,440 in memory that remembers where you've been. 21 00:01:38,440 --> 00:01:42,000 So for example, PWD, Home-Platoo, okay? 22 00:01:42,000 --> 00:01:49,120 Well, Push-D documents, PWD, Home-Clatoo documents. 23 00:01:49,120 --> 00:01:55,560 To see what's on the stack, use the Ders command, D-I-R-S. 24 00:01:55,560 --> 00:02:01,280 It says, tilde slash documents, space, tilde. 25 00:02:01,280 --> 00:02:07,720 It's listing my history on one line, starting with the most recent that is your current 26 00:02:07,720 --> 00:02:15,680 location of in this example, tilde documents, followed by another tilde for the Home-Directory 27 00:02:15,680 --> 00:02:17,680 because that's where I started from. 28 00:02:17,680 --> 00:02:23,960 You can add more to your stack by moving around your file system some more, so for instance, 29 00:02:23,960 --> 00:02:30,920 Push-D, tilde slash pictures, Ders, tilde pictures, tilde documents, tilde. 30 00:02:30,920 --> 00:02:37,480 To pop a directory off the stack and make it your current location, use the pop D command. 31 00:02:37,480 --> 00:02:43,040 The pop D command removes the most recent directory from the stack and makes it your current 32 00:02:43,040 --> 00:02:50,000 location and displays your new stack with the left directory representing your new location. 33 00:02:50,000 --> 00:02:57,600 So D-I-R-S, tilde pictures, tilde documents, tilde, pop D, all right, I don't even have 34 00:02:57,600 --> 00:03:00,800 to type in, Ders, it tells me, tilde documents, tilde. 35 00:03:00,800 --> 00:03:06,800 So to condense that down into one sentence explanation, Push-D, to change a directory to 36 00:03:06,800 --> 00:03:13,760 a new place, pop D, to take a directory off of your stack and make it your new current location. 37 00:03:13,760 --> 00:03:15,960 Ders to a view your stack. 38 00:03:15,960 --> 00:03:22,400 Next, CD space dash, I know, I said without CD, but this is with CD, but there's a slight 39 00:03:22,400 --> 00:03:23,400 difference here. 40 00:03:23,400 --> 00:03:26,120 This is maybe some people don't know about this little trick. 41 00:03:26,120 --> 00:03:30,320 When you find yourself switching back and forth between two directories, you do not have 42 00:03:30,320 --> 00:03:32,240 to type the directories each time. 43 00:03:32,240 --> 00:03:39,440 So don't even have to use control P or the up arrow, you can use CD-Dash. 44 00:03:39,440 --> 00:03:43,200 The CD-Dash command takes you to your previous directory. 45 00:03:43,200 --> 00:03:45,960 It doesn't use the same stack as Push D does. 46 00:03:45,960 --> 00:03:52,080 It just remembers what your previous one directory was and then takes you to it and displays 47 00:03:52,080 --> 00:03:53,640 your current location. 48 00:03:53,640 --> 00:04:02,080 So if I'm in slash Etsy, I could do a CD slash var and then CD space dash on back in Etsy. 49 00:04:02,080 --> 00:04:08,600 CD space dash, I'm back in var, CD space dash, I'm back in Etsy, back and forth. 50 00:04:08,600 --> 00:04:10,360 Really, really convenient. 51 00:04:10,360 --> 00:04:12,560 Three variables. 52 00:04:12,560 --> 00:04:16,280 Sometimes you might find yourself returning or otherwise interacting with the same 53 00:04:16,280 --> 00:04:20,560 directory with a long and inconvenient name over and over again. 54 00:04:20,560 --> 00:04:27,360 For instance, suppose you're auditing some files in slash, or home, clout to slash dot 55 00:04:27,360 --> 00:04:32,040 local slash Etsy slash myder slash config slash exam. 56 00:04:32,480 --> 00:04:34,200 And then there are a bunch of files in there. 57 00:04:34,200 --> 00:04:39,880 Instead of typing some variation of e-max, home, clout to dot local slash Etsy slash 58 00:04:39,880 --> 00:04:45,960 mighty, myder config examples file one dot txt for every file you need to open, create 59 00:04:45,960 --> 00:04:49,480 a short variable and use it as your path instead. 60 00:04:49,480 --> 00:04:55,960 So export myder equals slash home slash clout to slash dot local slash Etsy slash 61 00:04:55,960 --> 00:04:57,720 myder slash config slash example. 62 00:04:57,720 --> 00:04:59,640 And then from then on, you can just use myder. 63 00:04:59,640 --> 00:05:07,000 So e-max dollar sign, myder slash file one dot txt done. 64 00:05:07,000 --> 00:05:11,560 The variable disappears when you reboot or you can destroy it manually when you're done 65 00:05:11,560 --> 00:05:13,760 with the unset command, unset myder. 66 00:05:13,760 --> 00:05:16,880 Now you don't have that variable or you could reset it to something else. 67 00:05:16,880 --> 00:05:23,200 Myder equals some other long directory and now you've got that one for to use for history 68 00:05:23,200 --> 00:05:24,880 and history verify. 69 00:05:24,960 --> 00:05:28,880 You might already know that the history command lets you see your past, I don't know, 70 00:05:28,880 --> 00:05:31,840 thousand or so commands as a numbered list. 71 00:05:31,840 --> 00:05:36,240 And you can use that list to instantly execute a previous command by number. 72 00:05:36,240 --> 00:05:41,440 The syntax is simply an exclamation mark followed by the number of the command you want 73 00:05:41,440 --> 00:05:42,440 to execute. 74 00:05:42,440 --> 00:05:50,400 So for example, if you do history, pipe head dash in three, you see maybe a CD tilde slash 75 00:05:50,400 --> 00:05:54,840 documents as command 758. 76 00:05:55,080 --> 00:06:04,680 Return and you've just changed directories to tilde, tilde document. 77 00:06:04,680 --> 00:06:11,920 But the hist verify option in the bash shell lets you verify a history line before you 78 00:06:11,920 --> 00:06:23,960 execute, activate it with shopped shell options, s-h-o-p-t, shopped space dash s to set hist verify 79 00:06:24,040 --> 00:06:28,240 that's h-i-s-t verify all one string. 80 00:06:28,240 --> 00:06:33,920 Now when you type in exclamation mark 758, instead of executing that command from your 81 00:06:33,920 --> 00:06:41,880 history, your next prompt contains the command and waits for you to either edit it or 82 00:06:41,880 --> 00:06:45,400 to accept it with a return or enter on your keyboard. 83 00:06:45,440 --> 00:06:55,000 So for me, that's not faster than just exclamation, you know, 801 or whatever, 500, 84 00:06:55,000 --> 00:06:56,000 whatever. 85 00:06:56,000 --> 00:07:00,920 But what it does is give, it gives me the confidence to actually use something from 86 00:07:00,920 --> 00:07:01,920 history. 87 00:07:01,920 --> 00:07:06,840 Because sometimes I'll be a little bit nervous that I'm forgetting the number of the history 88 00:07:06,840 --> 00:07:07,840 or something like that. 89 00:07:07,840 --> 00:07:10,240 And so I just, I won't, I'll end up not using it. 90 00:07:10,240 --> 00:07:15,240 But with that confirmation step before I actually do the thing, then I feel more comfortable 91 00:07:15,280 --> 00:07:20,120 about looking at history, remembering it, being confident that I remember it, and being 92 00:07:20,120 --> 00:07:25,640 confident that if I don't, I'll have a chance to not do the thing that I, that I thought 93 00:07:25,640 --> 00:07:27,440 I'm about to do. 94 00:07:27,440 --> 00:07:29,560 It also helps me edit commands. 95 00:07:29,560 --> 00:07:35,920 So if there is a long directory name and then I start using a similar name, you know, in 96 00:07:35,920 --> 00:07:41,800 that, a similar path, but just, just the, a different sub directory, then I can just edit 97 00:07:41,800 --> 00:07:46,560 that command with his to verify, edit it before I accept it. 98 00:07:46,560 --> 00:07:50,320 And now I've got a new command that I can start using out of my history. 99 00:07:50,320 --> 00:07:57,160 Five auto CD, the auto CD show option in bash lets you skip the CD command altogether. 100 00:07:57,160 --> 00:08:03,000 To change directory, you just type the path of a valid directory on your system, no CD 101 00:08:03,000 --> 00:08:04,000 required. 102 00:08:04,000 --> 00:08:05,000 It has to be enabled. 103 00:08:05,000 --> 00:08:10,440 So you, you can enable it with the same way as with his to verify, you do SHOPT, that's 104 00:08:10,440 --> 00:08:18,800 like shell option, SHOPT, space dash S to set, like Sierra, set, dash S, space auto 105 00:08:18,800 --> 00:08:19,800 CD. 106 00:08:19,800 --> 00:08:26,120 Now just type the path of a valid directory, so you can, like, slash TMP. 107 00:08:26,120 --> 00:08:28,800 Now I'm in the TMP folder, like it's that easy. 108 00:08:28,800 --> 00:08:32,040 There's no CD to, to type, it's, it's kind of nice. 109 00:08:32,040 --> 00:08:35,440 I mean, it's three fewer keyboard presses. 110 00:08:35,440 --> 00:08:38,800 And then finally, the last one, I could think of, and I, I hope someone can think of 111 00:08:38,800 --> 00:08:39,800 more. 112 00:08:39,800 --> 00:08:43,240 The last one, I could think of was not to change directory at all. 113 00:08:43,240 --> 00:08:48,080 It's a little bit of a cheat, but realistically, for a lot of the things on a Linux, in 114 00:08:48,080 --> 00:08:52,760 the terminal that you're doing in a, with a Linux command, you don't really, very often, 115 00:08:52,760 --> 00:08:55,800 you don't have to be in the place you're working. 116 00:08:55,800 --> 00:09:00,760 You can just open the file, or, or pipe something to the, or, like, redirect something 117 00:09:00,760 --> 00:09:05,960 to that file, or, or convert that file from a path to a path. 118 00:09:05,960 --> 00:09:08,480 You don't have to go there to do it. 119 00:09:08,480 --> 00:09:11,280 The command will do all of that legwork for you. 120 00:09:11,280 --> 00:09:19,000 I think I, I tend to go to, to, to, to directories, maybe more often than I probably should. 121 00:09:19,000 --> 00:09:22,000 I mean, admittedly, sometimes that's not the case. 122 00:09:22,000 --> 00:09:25,640 Maybe, sometimes it is better to be in the directory, because that way you only have 123 00:09:25,640 --> 00:09:30,240 to type the file name, or, you know, it, it's just, it's less to type, although, again, 124 00:09:30,240 --> 00:09:31,680 I could use a variable. 125 00:09:31,680 --> 00:09:36,640 So sometimes I just feel like I'm moving around too much, and, and lately, I have been 126 00:09:36,640 --> 00:09:41,520 trying to challenge myself a little bit, not, not, not too much, but a little bit, just 127 00:09:41,520 --> 00:09:47,240 when I think I want to do something to a file, or look at a file, or whatever, I don't 128 00:09:47,240 --> 00:09:48,240 see it either. 129 00:09:48,240 --> 00:09:52,400 I just do it from wherever I am, just, just work out of my home directory. 130 00:09:52,400 --> 00:09:57,000 It's kind of a fun challenge, sometimes, and that's, that's kind of what made me think 131 00:09:57,000 --> 00:10:03,360 of this was how, how could I get around CDing so much, because I've noticed that I 132 00:10:03,360 --> 00:10:06,960 do spend too much time, I think, CDing to places. 133 00:10:06,960 --> 00:10:14,560 It's kind of like, I've cut certainly, you know, a graphical file manager out of my life, 134 00:10:14,560 --> 00:10:18,360 not entirely, but, you know, if I don't, if I don't need one, if I don't want to use 135 00:10:18,360 --> 00:10:20,840 a graphical file manager, I don't have to. 136 00:10:20,840 --> 00:10:28,080 And so, I feel kind of like I replaced it with a bunch of CDing and L.S.ing a lot, and 137 00:10:28,080 --> 00:10:32,600 that's another, that's a new inefficiency, that, that just half of the time doesn't 138 00:10:32,600 --> 00:10:34,440 really need to be done. 139 00:10:34,440 --> 00:10:39,600 Sometimes it does, like I say, file paths can get long and cumbersome, and for one off, I mean, 140 00:10:39,600 --> 00:10:43,640 you may as well, CD there in L.S, because why not? 141 00:10:43,640 --> 00:10:46,400 Although maybe not, again, you could just L.S. 142 00:10:46,400 --> 00:10:52,120 So really, I think we may be possibly some people, myself, at least, we CD too much. 143 00:10:52,120 --> 00:10:59,960 So if you want to cut that down, think of a couple of ways or borrow my ways of not 144 00:10:59,960 --> 00:11:06,480 CDing, of not moving around your file system as much, you could save seconds of work 145 00:11:06,480 --> 00:11:07,480 for yourself. 146 00:11:07,480 --> 00:11:09,200 Thanks for listening to this episode. 147 00:11:09,200 --> 00:11:15,880 Go record your own on some cool tip that you have, talk to you next time. 148 00:11:15,880 --> 00:11:20,800 You have been listening to Hecker Public Radio, as Hecker Public Radio does work. 149 00:11:20,800 --> 00:11:26,160 Today's show was contributed by a HBO artist like yourself, if you ever thought of 150 00:11:26,160 --> 00:11:32,560 a recording podcast, click on our contributally to find out how easy it means. 151 00:11:32,560 --> 00:11:39,520 Hosting Prage VR has been kindly provided by an onsthost.com, the Internet Archive, and our 152 00:11:39,520 --> 00:11:41,040 Sync.net. 153 00:11:41,040 --> 00:11:47,240 On this otherwise stages, today's show is released on our Creative Commons, Attribution, 154 00:11:47,240 --> 00:11:49,240 4.0 International License.